<template>
   <el-card>
    <el-table border style="margin: 10px 0px;" :data="skuArr">
      <el-table-column label="序号" type="index" align="center" width="80px"></el-table-column>
      <el-table-column label="名称" show-overflow-tooltip prop="skuName" width="150px"></el-table-column>
      <el-table-column label="描述" show-overflow-tooltip prop="skuDesc" width="200px"></el-table-column>
      <el-table-column label="图片"  width="200px">
        <template #="{row,$index}">
          <img :src="row.skuDefaultImg" style="width: 100px;height: 100px;">
        </template>
      </el-table-column>
      <el-table-column label="重量" prop="weight" width="200px"></el-table-column>
      <el-table-column label="价格" prop="price" width="150px"></el-table-column>
      <el-table-column label="操作"  width="250px" fixed="right">
        <template #="{row,$index}">
          <el-button :type="row.isSale?'primary':'info'" size="small" :icon="row.isSale==1?'Bottom':'Top'" @click="updateSale(row)"></el-button>
          <el-button type="primary" size="small" icon="Edit" @click="updateSku"></el-button>
          <el-button type="primary" size="small" icon="InfoFilled" @click="findSku(row)"></el-button>
          <el-popconfirm :title="`你确定要删除${row.skuName}?`" width="200px" @confirm="removeSku(row.id)">
    <template #reference>
      <el-button type="primary" size="small" icon="Delete"></el-button>
    </template>
  </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[5, 10, 15, 20]"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
      @current-change="getHasSku"
      @size-change="handler"
    />
     <!--抽屉组件：展示商品详情-->
  <el-drawer v-model="drawer">
    <!--标题部分-->
    <template #header>
      <h4>查看商品的详情</h4>
    </template>
    <template #default>
      <el-row style="margin: 10px 0px;">
        <el-col :span="6">名称</el-col>
        <el-col :span="18">华为meta20</el-col>
      </el-row >
      <el-row style="margin: 10px 0px;">
        <el-col :span="6">描述</el-col>
        <el-col :span="18">华为YYDS</el-col>
      </el-row>
      <el-row style="margin: 10px 0px;">
        <el-col :span="6">价格</el-col>
        <el-col :span="18">3999</el-col>
      </el-row>
      <el-row style="margin: 10px 0px;">
        <el-col :span="6">平台属性</el-col>
        <el-col :span="18"><el-tag style="margin: 5px;" v-for="(item,index) in 10" :key="index">{{item}}</el-tag></el-col>
      </el-row>
      <el-row style="margin: 10px 0px;">
        <el-col :span="6">销售属性</el-col>
        <el-col :span="18"><el-tag style="margin: 5px;" v-for="(item,index) in 10" :key="index">{{item}}</el-tag></el-col>
      </el-row>
      <el-row style="margin: 10px 0px;">
        <el-col :span="6">商品图片</el-col>
        <el-col :span="18">
          <el-carousel :interval="4000" type="card" height="200px">
            <el-carousel-item v-for="item in 6" :key="item">
             <h3 text="2xl" justify="center">{{ item }}</h3>
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </template>
  </el-drawer>
   </el-card>
</template>

<script setup name="sku" lang="ts">
import {ref,onMounted} from 'vue';
import { reqRemoveSku,reqSkuInfo,reqSkuList, reqSaleSku,reqCancelSale} from '@/api/product/sku/index';
import type { SkuResponseData,SkuData,SkuInfoData} from '@/api/product/sku/type'
import { ElMessage } from 'element-plus';
//分页器当前页码
let pageNo=ref<number>(1)
//每一页展示几条数据
let pageSize= ref<number>(5)
//总共数据条数
let total=ref<number>(0)
let skuArr = ref<SkuData[]>([])
//控制抽屉的显示与隐藏
let drawer = ref<boolean>(false)
let skuInfo = ref<SkuData>({})
//组件挂载完毕
onMounted(()=>{
  getHasSku()
})

const getHasSku =async(pager=1)=>{
  //当前分页器的页码
  pageNo.value = pager;
 let result: SkuResponseData = await reqSkuList(pageNo.value,pageSize.value)
   if(result.code==200){
    total.value=result.data.total
    skuArr.value=result.data.records
   }
}
//分页器下拉菜单发生变化时触发
const handler = (pageSizes:number)=>{
   getHasSku()
}
//商品的上架与下架的操作
const updateSale=async(row:SkuData)=>{
  if(row.isSale==1){
    //下架操作
  let result:any = await reqCancelSale((row.id as number))
    //提示信息
    ElMessage({
      type:'success',
      message:'下架成功'
    })
    getHasSku(pageNo.value)
  }else{
    //下架操作
    let result1:any = await reqSaleSku((row.id as number))
    //提示信息
    ElMessage({
      type:'success',
      message:'上架成功'
    })
    getHasSku(pageNo.value)
  }
}
//更新已有的SKU
const updateSku = ()=>{
  ElMessage({
    type:"success",
    message:'程序员在努力的更新中......'
  })
}
//查看商品详情按钮的回调
const findSku =async(row:SkuData)=>{
  //抽屉展示出来
  drawer.value=true;
  //获取已有商品详情数据
  let result:SkuInfoData = await reqSkuInfo(row.id)
  console.log('查看商品详情',result)
}
//删除某一个已有的商品
const removeSku=async(id:number)=>{
  let result:any =await reqRemoveSku(id)
  if(result.code==200){
    //提示信息
    ElMessage({
      type:'success',
      message:'删除成功'
    })
    //获取已有全部商品
    getHasSku(skuArr.value.length>1?pageNo.value:pageNo.value-1)
  }else{
    ElMessage({
      type:'error',
      message:'系统数据不能删除'
    })
  }
}
</script>

<style lang="scss" scoped>
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>